var myMap;
var polyControlAdder;
var MapObject = function(){
	function getAddPolyClickHandler(line){
		var c = 0;
		var i = 0;
		var colArray;

		if (line){
			colArray = new Array(	new CIcon('009', '#557AAC'),
									new CIcon('016', '#55AC5D'),
									new CIcon('076', '#8E1616'),
									new CIcon('080', '#8C189F'),
									new CIcon('096', '#A8BC34'),
									new CIcon('106', '#9E530E'));		
		} else {
			colArray = new Array(	new CIcon('106', '#9E530E', '#9E530E'),
									new CIcon('096', '#A8BC34', '#A8BC34'),
									new CIcon('080', '#8C189F', '#8C189F'),
									new CIcon('076', '#8E1616', '#8E1616'),
									new CIcon('016', '#55AC5D', '#55AC5D'),
									new CIcon('009', '#557AAC', '#557AAC'));		
		}
		return function(){
			c = c + 1;
			if (c==colArray.length) {
				c = 0;
			}
			var name = (line)?"Polyline ":"Polygon ";
			name = name + i++;
			name=window.prompt("Name this " + (line)?"Polyline":"Polygon",name);

			var poly;
			if (name!=null) {
				poly = new PolygonRenderer(myMap, colArray[c],name);
				poly.activate();
			}
			return poly;
		};
	};
	function getAddPointClickHandler(){
		var c = 0;
		var i = 0;
		var colArray = new Array(	new CIcon('009'),
								new CIcon('016'),
								new CIcon('076'),
								new CIcon('080'),
								new CIcon('096'),
								new CIcon('106'));		

		return function(){
			c = c + 1;
			if (c==colArray.length) {
				c = 0;
			}
			var name = "Point " + i++;
			name=window.prompt("Name this Point",name);

			var point;
			if (name!=null) {
				point = new PointRenderer(myMap, colArray[c],name);
				point.activate();
			}
			return point;
		};
	};
	function getPolyControlAdder() {
		var polyControl = new PolyControls();
		alert("So whats so cool to see here? \n"+
			  "In about 5 seconds you will see the poly controls. \n" +
			  "Of course it could have been activated instantly, \n" +
			  "I thought this was more dramatic... " +
			  "I dont have any of the color things documented... \n" +
			  "but they change color based on what you can do with them.\n\n",
			  "INTERNET EXPLORER SUCKS!");
		var isOn = false;
		return function(){
			if (isOn) {
				myMap.removeControl(polyControl);
			} else {
				myMap.addControl(polyControl);
			}

		};

	}


    // return a public interface
    return {
    	init : function(){

    	    /*Ext.EventManager.on(document.body, 'unload', );*/
			myMap = new GMap2(document.getElementById('map-container'));
			var startingCenter = new GLatLng(38.92255806063647, -77.23161220550537);
			myMap.addControl(new GOverviewMapControl()); 
			myMap.addControl(new GLargeMapControl()); 
			myMap.addControl(new PolyControls());			
			myMap.addControl(new SearchControl()); 

			myMap.addControl(new GMapTypeControl());
			myMap.setCenter(startingCenter, 15);

			// polyControlAdder = getPolyControlAdder();
			// setTimeout("polyControlAdder()",3000);

			var polyClickHandler = getAddPolyClickHandler();
			var polyLineClickHandler = getAddPolyClickHandler(true);
			var pointClickHandler = getAddPointClickHandler();
			GEvent.addListener(myMap, "addPoly", polyClickHandler);
			GEvent.addListener(myMap, "addPolyline", polyLineClickHandler);
			GEvent.addListener(myMap, "addPoint", pointClickHandler);
			
        },
        showDialog : function(){}
    };
}();

